<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="Connect.*" %>

<%
	// Redirect to the home page if the user is not a manager.
	if (session.getAttribute("user_type") == null) {
		response.sendRedirect("no_permission.jsp");
	} else if (!session.getAttribute("user_type").equals("0")) {
		response.sendRedirect("no_permission.jsp");
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="style.css" media="screen" />
<title>Hammer Time Auctions</title>
<script language="javascript" type="text/javascript">
	function editClicked(button) {
		var myForm = document.createElement("form");
		myForm.setAttribute('method', "post");
		myForm.setAttribute('action', "edit_employee.jsp");
		var data = document.createElement("input");
		data.setAttribute('type', "hidden");
		data.setAttribute('name', "username");
		data.setAttribute('value', button.title);
		myForm.appendChild(data);
		javascript: myForm.submit();
	}
	function addClicked() {
		var myForm = document.createElement("form");
		myForm.setAttribute('method', "post");
		myForm.setAttribute('action', "add_new_user.jsp");
		var data = document.createElement("input");
		data.setAttribute('type', "hidden");
		data.setAttribute('name', "form_type");
		data.setAttribute('value', "Employee");
		myForm.appendChild(data);
		javascript: myForm.submit();
	}
</script>
</head>
<body>
	<div id="content">
		<%@include file="header.jsp"%>
		<h1>Manage Employees</h1>
		<hr></hr>
		<h2>List Of Employees</h2>
		<table class="center data-table">
			<%

				// Getting employee information
				java.sql.Connection conn = null;
				try {
					// Connect to the database.
					Class.forName(ConnectionInfo.myJDBCDriver()).newInstance();
					java.util.Properties sysprops = System.getProperties();
					sysprops.put("user", ConnectionInfo.myUserID());
					sysprops.put("password", ConnectionInfo.myPassword());
					conn = java.sql.DriverManager.getConnection(ConnectionInfo.myURL(), sysprops);

					// Display a table of employee information.
					java.sql.Statement statement = conn.createStatement();
					java.sql.ResultSet rs = statement.executeQuery("SELECT users.*, employees.start_date, employees.hourly_wage, employees.level, employees.ssn "
							+ "FROM users RIGHT JOIN employees ON users.username = employees.username ORDER BY users.username");
					java.sql.ResultSetMetaData columns = rs.getMetaData();
					out.println("<tr>");
					for (int i = 1; i <= columns.getColumnCount(); i++) {
						out.println("<th>" + columns.getColumnName(i) + "</th>");
					}
					out.println("<th></th>");
					out.println("</tr>");
					while (rs.next()) {
						out.println("<tr>");
						for (int i = 1; i <= columns.getColumnCount(); i++) {
							out.println("<td>" + rs.getString(columns.getColumnName(i)) + "</td>");
						}
						out.println("<td><a href=\"#\" class=\"button\" title=" + rs.getString(columns.getColumnName(1))
								+ " onclick=\"return editClicked(this)\">Edit</a></td>");
						out.println("</tr>");
					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					try {
						conn.close();
					} catch (Exception ee) {
					}
				}
			%>
		</table>
		<table class="center data-table">
			<tr>
				<td><a href="#" class="right button" onclick="return addClicked()">
						+ New User</a></td>
			</tr>
		</table>
	</div>
	<hr></hr>
	<%@include file="footer.jsp"%>
</body>
</html>